package com.sns.connect.ui;

import java.util.List;
import java.util.regex.Pattern;

import com.sns.connect.R;

import android.content.ContentUris;
import android.content.Context;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ContactsListAdapter extends BaseAdapter {
	
	private Context mContext;
	private List<ContactInfo> mItems;
	private LayoutInflater mInflater;
	private static final String TAG_NO_SORT = "no_sort"; 
	
	public static class ContactInfo {
		String sort_key;
		String name;
		String phone;
		String photo;
		int resId;
		int position;
		boolean selected;
	}

	private class ListItemView extends LinearLayout {
		private int mIndex;
		private TextView mAlpha;
		private ImageView mPortrait;
		private TextView mName;
		private TextView mPhone;
		private ImageView mSelect;

		public ListItemView(Context context) {
			super(context);

			mInflater.inflate(R.layout.contacts_list_item, this, true);
			mAlpha = (TextView) findViewById(R.id.contact_list_alpha);
			mPortrait = (ImageView) findViewById(R.id.contact_list_portrait);
			mName = (TextView) findViewById(R.id.contact_list_name);
			mPhone = (TextView) findViewById(R.id.contact_list_phone);
			mSelect = (ImageView) findViewById(R.id.contact_list_select);
		}
		
		public void setInfo(ContactInfo info, int index) {
			mIndex = index;
			if (info.photo != null) {
				//PHOTO_ID as param;
				Uri portrait =  ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, Long.parseLong(info.photo));
				//Uri portrait =  Uri.parse(info.photo);
				mPortrait.setImageURI(portrait);
			}
			if (info.photo == null) {
				mPortrait.setImageResource(R.drawable.ic_portrait);
			}
			mName.setText(info.name);
			mPhone.setText(info.phone);
			
			if(info.selected == true)
				mSelect.setVisibility(View.VISIBLE);
			else
				mSelect.setVisibility(View.INVISIBLE);
			
			if (mItems.get(mIndex).sort_key.equals(TAG_NO_SORT)) {
				mAlpha.setVisibility(View.GONE);
				return;
			}
			
			String currentStr = getAlpha(mItems.get(mIndex).sort_key);
            String previewStr = (mIndex - 1) >= 0 ? getAlpha(mItems.get(mIndex - 1).sort_key) : " ";
            if (!previewStr.equals(currentStr)) {  
                mAlpha.setVisibility(View.VISIBLE);
                mAlpha.setText(currentStr);
            } else {  
                mAlpha.setVisibility(View.GONE);
            }
		}
		public String getAlpha(String str) {  
	        if (str == null) {  
	            return "#";  
	        }  
	  
	        if (str.trim().length() == 0) {  
	            return "#";  
	        }  
	  
	        char c = str.trim().substring(0, 1).charAt(0);  
	        Pattern pattern = Pattern.compile("^[A-Za-z]+$");  
	        if (pattern.matcher(c + "").matches()) {  
	            return (c + "").toUpperCase();  
	        } else {  
	            return "#";  
	        }  
	    }
	}
	
	public ContactsListAdapter(Context context, List<ContactInfo> listItems) {
		mContext = context;
		mInflater = LayoutInflater.from(context);
		mItems = listItems;
	}

	public void setListItems(List<ContactInfo> listItems) {
		mItems = listItems;
	}
	
	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public Object getItem(int position) {
		return mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ContactInfo info = (ContactInfo)getItem(position);

		if (convertView == null) {
			convertView = new ListItemView(mContext);
		}
			
		ListItemView view = (ListItemView)convertView;
		view.setInfo(info, position);

		return convertView;
	}
}

